<?php
/**
 * Copyright (C), 2016-2018, Shall Buy Life info. Co., Ltd.
 * FileName:OrderService.php
 * Description: 说明
 *
 * @author lwl
 * @Create Date 2021/12/01 13:15:40
 * @Update Date 2021/12/01 13:15:40
 * @version v1.0
 */

declare(strict_types=1);

namespace Example\Services\Processor\Refund;

use Example\Constants\RefundConstant;
use Example\Models\Refund;
use Example\Services\EcService;
use Example\Services\TccService;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

final class RefundService implements RefundConstant
{

    protected static $instance = null;

    public static function getInstance()
    {

        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 防止 new 多个实列
     * 必须私有，别问为啥 上面说原因了
     */
    private function __construct()
    {

    }

    /**
     * 防止clone 多个实列
     * 必须私有，别问为啥 上面说原因了
     */
    private function __clone()
    {

    }

    /**
     * FunctionName：__wakeup
     * Description：
     * Author：lwl
     */
    private function __wakeup()
    {

    }

    /**
     * FunctionName：store
     * Description：
     * Author：lwl
     * @param string $orderSn
     * @param $refundMoney
     * @return bool[]|false[]
     */
    public function store(string $orderSn, $refundMoney): array
    {
        $result = TccService::storeRefund($orderSn, $refundMoney);
        if ($result['code'] != Response::HTTP_OK) {
            return ['result' => false];
        }
        return ['result' => true];
    }


    /**
     * FunctionName：agree
     * Description：同意退款
     * Author：lwl
     * @param string $refundSn
     * @param int $refundWay
     * @return array
     */
    public function agree(string $refundSn, int $refundWay): array
    {
        $result = EcService::refundAgree('decoration.order.refund.create', $refundSn, $refundWay);
        if ($result['code'] != Response::HTTP_OK) {
            return ['result' => false];
        }
        return ['result' => true];
    }

    /**
     * FunctionName：callBack
     * Description：退款回调
     * Author：lwl
     * @param array $params
     * @return bool[]|false[]
     */
    public function callBack(array $params): array
    {
        // TODO: Implement callBack() method.

        DB::beginTransaction();
        try {
            $query = Refund::query()->where('refund_sn', $params['refund_sn']);
            if ($params['status'] == 1) {
                $update = ['status' => RefundConstant::STATUS_REFUND_FINISH, 'finished_at' => date('Y-m-d H:i:s')];
            } else {
                $update = ['status' => RefundConstant::STATUS_REFUND_REJECT];
            }
            if (!$query->update($update)) {
                DB::rollBack();
                Log::channel('refund')->info('退款订单-回调-错误', ['订单号' => $params['order_sn'], '退款单号' => $params['refund_sn']]);
                return ['result' => false];
            }
        } catch (\Exception $exception) {
            DB::rollBack();
            Log::channel('refund')->info('退款订单-回调-异常', ['订单号' => $params['order_sn'], '退款单号' => $params['refund_sn'], 'message' => $exception->getMessage()]);
            return ['result' => false];
        }
        DB::commit();
        return ['result' => true];
    }
}
